package com.saint.news;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.saint.news.Utils.AppUtils;
import com.saint.news.Utils.HttpUtil;
import com.saint.news.adapter.CatPagerAdapter;
import com.saint.news.bean.CatContent;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2018/3/3.
 */

public class SubContentAct extends AppCompatActivity {

    private Activity act;
    private Toast toast;
    private ViewPager viewPager;
    private CatContent catContent;
    private List<CatContent.DataBean> dataBeans;
    private Callback<String> subCatContentCall;
    private TextView tvTitle, tvPagerNum, tvBack, tvEmpty;
    private ImageView ivLeft, ivRight;
    private Drawable loadingDrawable;
    private String title;
    private String titleColor;
    private int titleMarginTop;
    private int titleMarginLeft;
    private int titleSize;
    private Call<String> catContentCall;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_sub_cotent);
        initView();
        initCallBack();
        getSubCatContent();
    }

    public static Intent newIntent(Activity act, String title, String titleColor, String catId,
                                   int titleMarginTop, int titleMarginLeft, int titleSize) {
        Intent intent = new Intent(act, SubContentAct.class);
        intent.putExtra("title", title);
        intent.putExtra("title_color", titleColor);
        intent.putExtra("cat_id", catId);
        intent.putExtra("margin_top", titleMarginTop);
        intent.putExtra("margin_left", titleMarginLeft);
        intent.putExtra("title_size", titleSize);
        return intent;
    }

    private void initView() {
        act = this;
        viewPager = findViewById(R.id.view_pager_content);
        tvTitle = findViewById(R.id.tv_title);
        tvPagerNum = findViewById(R.id.tv_pager_num);
        ivLeft = findViewById(R.id.iv_left);
        ivRight = findViewById(R.id.iv_right);
        tvBack = findViewById(R.id.tv_back);
        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvEmpty = findViewById(R.id.tv_empty);
    }


    private void initCallBack() {
        subCatContentCall = new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful() && null != response.body()) {
                    Log.e("HTTP", "请求结果： " + response.body());
                    catContent = new Gson().fromJson(response.body(), CatContent.class);
                    dataBeans = catContent.getData();
                    if (dataBeans.size() == 0) {
                        tvEmpty.setVisibility(View.VISIBLE);
                        tvPagerNum.setVisibility(View.GONE);
                        ivLeft.setVisibility(View.GONE);
                        ivRight.setVisibility(View.GONE);
                    } else {
                        setAdapterToViewPager(catContent.getData());
                        tvPagerNum.setText(1 + " / " + dataBeans.size());
                        tvEmpty.setVisibility(View.GONE);
                        tvPagerNum.setVisibility(View.VISIBLE);
                        ivLeft.setVisibility(View.VISIBLE);
                        ivRight.setVisibility(View.VISIBLE);
                    }
                } else {
                    onFailure(call, new Throwable("response body is null"));
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                tShort("加载失败：" + t.getMessage());
                Log.e("LOADINGData", t.getMessage());
            }
        };
    }


    public void tShort(String msg) {
        if (null == toast) {
            toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        } else {
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setText(msg);
        }

        toast.show();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        AppUtils.play();
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    private void getSubCatContent() {
        loadingDrawable = getResources().getDrawable(R.drawable.loading);
        title = getIntent().getStringExtra("title");
        titleColor = getIntent().getStringExtra("title_color");
        titleMarginTop = getIntent().getIntExtra("margin_top", 0);
        titleMarginLeft = getIntent().getIntExtra("margin_left", 0);
        titleSize = getIntent().getIntExtra("title_size", 0);
        String catId = getIntent().getStringExtra("cat_id");


        Map<String, String> map = new HashMap<>();
        map.put("cat_id", catId);

        catContentCall = HttpUtil.getInstance(MainActivity.url).getSubCatContent(map);
        catContentCall.enqueue(subCatContentCall);

        if (!TextUtils.isEmpty(titleColor) && titleColor.length() == 6) {
            tvTitle.setTextColor(Color.parseColor("#" + titleColor));
        }
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tvTitle.getLayoutParams();
        if (titleMarginLeft > 0) {
            params.leftMargin = titleMarginLeft;
            params.rightMargin = titleMarginLeft;
        }
        if (titleMarginTop > 0) {
            params.topMargin = titleMarginTop;
        }
        tvTitle.setLayoutParams(params);
        if (titleSize > 0) {
            tvTitle.setTextSize(titleSize);
        }
        tvTitle.setText(title);
    }


    private void setAdapterToViewPager(final List<CatContent.DataBean> dataBeans) {

        viewPager.setAdapter(new CatPagerAdapter(act, dataBeans));

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tvPagerNum.setText(position + 1 + " / " + dataBeans.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void onDestroy() {
        if (catContentCall != null) {
            catContentCall.cancel();
        }
        super.onDestroy();
    }
}
